#include "gui_remove.h"
//
/*  Save file as gui_remove.cpp  */
/*  incomming class name Gui_Remove */
//
#include <QCloseEvent>

Gui_Remove::Gui_Remove( NetworkDav * db , QString path )
{
	setupUi( this );
    label_2->setText("");
    QApplication::restoreOverrideCursor();
    path = path;
    dbdav = db;
    passaggiwake = 0;
    dummyroot = dbdav->DummiRoot();
    aborter = false;
    redirpath = path;
    startpath = path;
    remotepath = RealRemotePath(path);
    label->setText(remotepath);
    onedirup = CDDirUp(path);
    //////////qDebug() << "#### constructor ############" << remotepath;
    //////////qDebug() << "#### dummyroot ############" << dummyroot;
    
    
    if (!path.endsWith("/")) {
    StopandCloseAll();
    } else {
    ContinueCicles();
    }
    
}
//
void Gui_Remove::Load_Connector()
{
    Del_File.clear();
    Del_Dir.clear();
    Del_Work_Grep.clear();
    CodaonDelete.clear();
    
    
    connect(dastop, SIGNAL(clicked()), this, SLOT(StopCicles()));
    connect(dago, SIGNAL(clicked()), this, SLOT(ContinueCicles()));
    InfoPath(startpath,0);
}



void Gui_Remove::InfoPath( QString dir , int level )
{
    if (!aborter) {
        
        //////////qDebug() << "#### GET INFO from ############" << dir;
        
        label->setText(tr("Get info from: %1").arg(RealRemotePath(dir)));
        label_2->setText("");
            DavPROPFIND *listin = new DavPROPFIND(dbdav,dir);
                  listin->SetFormat(INFODIRLISTFORMAT,level);
     connect(listin, SIGNAL(WarningMsg(QString)), this, SLOT(ShowMessageWarning(QString)));
     connect(listin, SIGNAL(InfoDavList(QStringList,QStringList,int)), this, SLOT(RegisterRecursive(QStringList,QStringList,int)));
    } else {
    StopCicles();
    }
}



void Gui_Remove::LastRemove()
{
    if (!aborter) {
        if (startpath == dummyroot) {
         StopCicles();
         return;            
        }
        
        Del_File.clear();
        Del_Dir.clear();
        Del_Work_Grep.clear();
        CodaonDelete.clear();
        
        
        
        const QString lastdir =  RealRemotePath(startpath);
        
        label->setText(tr("Pending operation on: %1").arg(lastdir));
        label_2->setText(tr("Last Remove: %1").arg(lastdir));
        
            DavDelete *listrml = new DavDelete(dbdav,lastdir);
            connect(listrml, SIGNAL(WarningMsg(QString)), this, SLOT(ShowMessageWarning(QString)));
            connect(listrml, SIGNAL(Confirmer()), this, SLOT(StopandCloseAll()));
    } else {
    StopCicles();
    }
}



/*

 QStringList Del_File;
    QStringList Del_Dir;
    QStringList Del_Work_Grep;

*/

void  Gui_Remove::RegisterRecursive(QStringList xdir ,QStringList xfile , int level )
{
    Del_Work_Grep.clear();
    
    
     for (int i = 0; i < xdir.size(); ++i)  {
                  if ( xdir.at(i) !=startpath and xdir.at(i) != dbdav->DummiRoot()  ) {
                      if (!Del_Dir.contains(xdir.at(i)) ) {
                       Del_Dir.append(xdir.at(i));  
                       Del_Work_Grep.append(xdir.at(i));
                      }
                      
                  }        
     }
     for (int i = 0; i < xfile.size(); ++i)  {
                      if (!Del_File.contains(xfile.at(i))) {
                      Del_File.append(xfile.at(i));  
                      }
                         
     } 
     //////////qDebug() << "#### Del_File.size()  ->" << Del_File.size();
     //////////qDebug() << "#### Del_Dir.size()  ->" << Del_Dir.size();
     //////////qDebug() << "#### Del_Work_Grep.size()  ->" << Del_Work_Grep.size();
     
     
     if (Del_Work_Grep.size() > 0 ) {
        GrabNextlevel( level + 1 );
        return;
     }
      
     
     int ddfile = Del_File.size();
     int dddir = Del_Dir.size();
     
     int summer = ddfile + dddir;
     
     if (summer > 0) {
     WakeUpsFile();
     return;
     }
     

     LastRemove();

}





 void Gui_Remove::GrabNextlevel( int level )
{
    NextInfoDir(0,level);
}

 void Gui_Remove::NextInfoDir( int now , int level  )
{
    
    if (aborter) { 
        return;
    }
    
    int bigmap = Del_Work_Grep.size();
    if (bigmap < 0) {
    Del_Work_Grep.clear();
    RegisterRecursive(Del_Work_Grep,Del_Work_Grep,level + 1);
    return;
    }
    
    if (now >= bigmap) { 
    Del_Work_Grep.clear();
    RegisterRecursive(Del_Work_Grep,Del_Work_Grep,level + 1);
    return;
    }
      for (int i = 0; i < Del_Work_Grep.size(); ++i)  {
                      if (now == i) {
                         const QString anextpath =  Del_Work_Grep.at(i);
                          if (anextpath.size() > 0) {
                          
                           InfoPath(anextpath,level);
                          } else {
                            NextInfoDir(now + 1 ,level); 
                          }
                      return;
                      }
     }
}



 void Gui_Remove::WakeUpsFile()
{
      CodaonDelete.clear();
      passaggiwake++;
      //////////qDebug() << "####LISTTOBE REMOVE ->" << passaggiwake << "-" << startpath;
    
      if ( passaggiwake >= 2) {
       return;
      }          
    
      QStringList RFile = ReverseList(Del_File);
    
       for (int i = 0; i < RFile.size(); ++i) {
           //////////qDebug() << "###file del   -> " << RFile.at(i);
           CodaonDelete.append(RFile.at(i));
       }
         
      QStringList RDir = ReverseList(Del_Dir);
    
       for (int i = 0; i < RDir.size(); ++i) {
           //////////qDebug() << "###dir del  -> " << RDir.at(i);
           CodaonDelete.append(RDir.at(i));
       }
       
        Del_File.clear();
        Del_Dir.clear();
        Del_Work_Grep.clear();
       
       if (CodaonDelete.size() > 0) {
         CodaRemove(0);    
       }
}


 void Gui_Remove::CodaRemove( int now   )
{
    
     if (aborter) {
        return;
    }
    
    int bigmap = CodaonDelete.size();
    if (bigmap == 0) {
    CodaonDelete.clear();
    LastRemove();
    return;
    }
    
    if (now >= bigmap) { 
    CodaonDelete.clear();
    LastRemove();
    return;
    }
      for (int i = 0; i < CodaonDelete.size(); ++i)  {
                      if (now == i) {
                          QString anextpath =  RealRemotePath(CodaonDelete.at(i));
                          if (anextpath.size() > 0) {
                              
                               label->setText(tr("Pending operation on: %1").arg(RealRemotePath(startpath)));
                               label_2->setText(tr("Remove wait: %1").arg(anextpath));
                              
                              
                          DavDelete *lismix = new DavDelete(dbdav,anextpath);
                                     lismix->SetPosition(now);
                            connect(lismix, SIGNAL(WarningMsg(QString)), this, SLOT(ShowMessageWarning(QString)));
                            connect(lismix, SIGNAL(ConChain(int)), this, SLOT(CodaRemove(int)));
                          } else {
                            CodaRemove(now + 1); 
                          }
                      return;
                      }
     }
}





 void Gui_Remove::ChoiserAction()
{
    
}

/*
connect(dastop, SIGNAL(clicked()), this, SLOT(StopCicles()));
    connect(dago, SIGNAL(clicked()), this, SLOT(ContinueCicles()));

*/

 void Gui_Remove::ContinueCicles()
{
    aborter = false;
    label_2->setText("");
    dastop->setDisabled(false);
    dago->setDisabled(true);
    Load_Connector();
}


 void Gui_Remove::StopCicles()
{
    aborter = true;
    label_2->setText(tr("On Pause......"));
    dastop->setDisabled(true);
    dago->setDisabled(false);
}


 void Gui_Remove::ShowMessageWarning( QString msg )
{
    StopCicles();
    QMessageBox::warning(this, tr("Error from Gui DELETE Dav_Module!"),msg);
    
}



 void Gui_Remove::StopandCloseAll()
{
    //////////qDebug() << "###closeEvent  ok ";
    StopCicles();
    emit CloseDeleteAction(startpath);
    accept();
}



QString Gui_Remove::RealRemotePath( QString dirorfile )
{
    QString remotefilefolder = QString("%1%2").arg(dbdav->DummiRoot()).arg(dirorfile);
    return remotefilefolder.replace("//","/");
}




void Gui_Remove::closeEvent( QCloseEvent* e )
{
   
	e->accept();
}

